﻿
//this is the id of the candidate when
//'Add Application' is clicked from Candidate View
var ApplicationDlg_CurrentCandidateID = -1;

//these need to be reset each time the dlg opens
var ApplicationDlg_SelectedCandidateID = -1;
var ApplicationDlg_SelectedPositionID = -1;
var ApplicationDlg_SelectedStateID = -1;

$(function () {
    //create an add candidate popup
    $("#AddApplicationPopup").dialog({
        modal: true,
        resizable: false,
        width: DEFAULT_DLG_WIDTH,
        height: DEFAULT_DLG_HEIGHT,
        position: ['center', 'center'],
        autoOpen: false,
        buttons:
        [
            {
                text: "Add",
                click: function () { OnAddApplicationClicked(); }
            },
            {
                text: "Cancel",
                click: function () { $(this).dialog("close"); }
            }
        ],
        title: "New Application",
        open: OnAddApplicationDlgOpen
    });

    // Make the Candidate box a jQuery UI Combobox
    $("#AppPopup_Candidate").combobox({
        valueselect: function (event, ui) { ApplicationDlg_SelectedCandidateID = ui.item.itemID; },
        source: []
    });

    // Make the Position box a jQuery UI Combobox
    $("#AppPopup_Position").combobox({
        valueselect: function (event, ui) { ApplicationDlg_SelectedPositionID = ui.item.itemID; },
        source: []
    });

    // Make the State box a jQuery UI Combobox
    $("#AppPopup_State").combobox({
        valueselect: function (event, ui) { ApplicationDlg_SelectedStateID = ui.item.itemID; },
        source: []
    });

    $("#AppPopup_CandidateCombobox").find("input").attr("readonly", "readonly");
    $("#AppPopup_PositionCombobox").find("input").attr("readonly", "readonly");
    $("#AppPopup_StateCombobox").find("input").attr("readonly", "readonly");
});


// Load the positions and states into their respective comboboxes
function OnAddApplicationDlgOpen() {
    // Get everything we need right when the dialog box is opened
    // Hopefully this will fix the loading issue
    var candidate_Json;
    var position_Json;
    var state_Json;

    ApplicationDlg_SelectedCandidateID = -1;
    ApplicationDlg_SelectedPositionID = -1;
    ApplicationDlg_SelectedStateID = -1;

    // clear errors
    $("#AppPopup_CandidateCombobox").removeClass("ui-state-error");
    $("#AppPopup_PositionCombobox").removeClass("ui-state-error");
    $("#AppPopup_StateCombobox").removeClass("ui-state-error");

    //clear the candidate
    $("#AppPopup_CandidateCombobox").find("input").first().val("");

    PageMethods.GetAddAppDlgData_Json(
        function (json) {
            candidate_Json = (eval(json)[0]);
            position_Json = (eval(json)[1]);
            state_Json = (eval(json)[2]);

            // Send the data to the Positions Combobox
            $("#AppPopup_Candidate").combobox("option", "source", candidate_Json);
            // Send the data to the Positions Combobox
            $("#AppPopup_Position").combobox("option", "source", position_Json);
            // Send the data to the State Combobox
            $("#AppPopup_State").combobox("option", "source", state_Json);

            //now select a candidate if this call originated in Candidate View
            if (ApplicationDlg_CurrentCandidateID > -1) {
                var candName = "";
                //find the current candidate name from id
                for (var i in candidate_Json) {
                    if (candidate_Json[i].itemID == ApplicationDlg_CurrentCandidateID) {
                        candName = candidate_Json[i].value;
                    }
                }

                //set the value in the combo box
                $("#AppPopup_CandidateCombobox").find("input").first().val(candName);
            }
        },
        function () {
            // TODO: SHOW ERROR MESSAGE
            $("#AppPopup_CandidateCombobox").addClass("ui-state-error");

            //set empty data
            $("#AppPopup_Candidate").combobox("option", "source", []);

            // TODO: SHOW ERROR MESSAGE
            $("#AppPopup_PositionCombobox").addClass("ui-state-error");

            //set empty data
            $("#AppPopup_Position").combobox("option", "source", []);

            // TODO: SHOW ERROR MESSAGE
            $("#AppPopup_StateCombobox").addClass("ui-state-error");

            //set empty data
            $("#AppPopup_State").combobox("option", "source", []);
        }
    );

    } // END: OnAddApplicationDlgOpen

    function OnAddApplicationClicked() {
        //clear out any messages
        $("#AppPopup_Message").addClass("ui-helper-hidden");
        $("#AppPopup_Error").addClass("ui-helper-hidden");

        var candID = $("#AppPopup_CandidateCombobox").find("input").first();
        var position = $("#AppPopup_PositionCombobox").find("input").first();
        var state = $("#AppPopup_StateCombobox").find("input").first();

        //validate the data
        var bValid = true;

        //name can't be empty
        // TODO: Should not be able to add a new candidate from here
        if (ApplicationDlg_CurrentCandidateID == -1 &&
            ApplicationDlg_SelectedCandidateID == -1) { // Need to click a candidate
            candID.addClass("ui-state-error");
            bValid = false;
        }
        else {
            candID.removeClass("ui-state-error");
        }

        // Position can't be empty
        // TODO: Add new position from here, WARN
        if (ApplicationDlg_SelectedPositionID == -1) {
            position.addClass("ui-state-error");
            bValid = false;
        }
        else {
            position.removeClass("ui-state-error");
        }

        // State can't be empty
        // Add new state from here, WARN
        if (ApplicationDlg_SelectedStateID == -1) {
            state.addClass("ui-state-error");
            bValid = false;
        }
        else {
            state.removeClass("ui-state-error");
        }

        // Add the application to the selected candidate
        if (bValid) {

            //we know both of these aren't -1, so tell the dialog to use the
            //auto-selected candidate
            if (ApplicationDlg_SelectedCandidateID == -1) {
                ApplicationDlg_SelectedCandidateID = ApplicationDlg_CurrentCandidateID;
            }

            // call page methods to add candidate
            PageMethods.AddApplication(
                ApplicationDlg_SelectedCandidateID,
                ApplicationDlg_SelectedPositionID,
                ApplicationDlg_SelectedStateID,
                function () {
                    //TODO Show success msg???

                    //close the dlg
                    $("#AddApplicationPopup").dialog("close");

                    location.reload(true); //refresh page for now
                },
                function () {
                    //error, show message and don't close dlg
                    $("#AppPopup_Error").removeClass("ui-helper-hidden");
                }
            );
        }
        else {
            //not valid, show message
            $("#AppPopup_Message").removeClass("ui-helper-hidden");
        }
    }
